package xyz.syyrjx.blog.service.tools.impl;

import org.springframework.stereotype.Service;
import xyz.syyrjx.blog._enum.State;
import xyz.syyrjx.blog.dao.tool.game2048.Score2048Dao;
import xyz.syyrjx.blog.entity.Score2048;
import xyz.syyrjx.blog.service.tools.Game2048Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 大地崩坏苍蝇兽
 * @date 2023/12/27 19:06
 * @description ...
 * @since v0.0.1
 */
@Service
public class Game2048ServiceImpl implements Game2048Service {

    @Resource
    private Score2048Dao score2048Dao;


    @Override
    public List<Score2048> getScoreFirstFive() {
        return score2048Dao.selectFirstFive();
    }

    @Override
    public void addScore(Score2048 score2048) {
        score2048.setState(State.NORMAL_STATE.getCode());
        score2048Dao.insertScore(score2048);
    }

    @Override
    public int getRank(int score) {
        int rank = score2048Dao.selectRank(score);
        int count = score2048Dao.countAll();
        if (count == 0) {
            return 100;
        }
        double radio = 1.0d * rank / count;
        return (int)(radio * 100);
    }
}
